﻿using System.Data;
using System.Web;
using System.Web.Services;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.Excel;

namespace eCommerce.cms.handler
{    
    public class GetExcel : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var sizeType   = Utils.GetIdFromRequestString(Utils.TypeId, 0);
            var categoryId = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            var exportFile = context.Server.MapPath("~/Data/UserData/1/Template.xlsx");

            if (sizeType > 0 && categoryId > 0)
            {
                var data = ProductBo.GetListByCategoryId(categoryId);
                
                var lstSize = SizeBo.GetListByType(sizeType);
                foreach (var size in lstSize)
                    data.Columns.Add(size.Name);

                var export = new ExcelExport();
                export.ExportDataTable(data, exportFile);
            }

            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=ImportOrExport.xlsx");
            context.Response.WriteFile(exportFile);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}